package com.bjsxt.controller.system;

import com.bjsxt.domain.Role;
import com.bjsxt.dto.RoleDto;
import com.bjsxt.service.RoleService;
import com.bjsxt.utils.ShiroSecurityUtils;
import com.bjsxt.vo.AjaxResult;
import com.bjsxt.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统角色控制器
 */
@RestController
@RequestMapping("system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询角色
     *
     * @param roleDto
     * @return
     */
    @GetMapping("listRoleForPage")
    public AjaxResult listRoleForPage(RoleDto roleDto) {
        DataGridView dataGridView = this.roleService.listRolePage(roleDto);
        return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
    }

    /**
     * 查询全部有效角色
     *
     * @return
     */
    @GetMapping("selectAllRoles")
    public AjaxResult selectAllRoles() {
        List<Role> roles = this.roleService.listAllRoles();
        return AjaxResult.success(roles);
    }

    /**
     * 根据角色id查询角色
     *
     * @param roleId
     * @return
     */
    @GetMapping("getRoleById/{roleId}")
    public AjaxResult getRoleById(@PathVariable Long roleId) {
        Role role = this.roleService.getOne(roleId);
        return AjaxResult.success(role);
    }

    /**
     * 添加角色
     *
     * @param roleDto
     * @return
     */
    @PostMapping("addRole")
    public AjaxResult addRole(@Validated RoleDto roleDto) {
        roleDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.roleService.addRole(roleDto));
    }

    /**
     * 修改角色
     *
     * @param roleDto
     * @return
     */
    @PutMapping("updateRole")
    public AjaxResult updataRole(@Validated RoleDto roleDto) {
        roleDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        return AjaxResult.toAjax(this.roleService.updateRole(roleDto));
    }

    /**
     * 根据ids删除角色【同时删除sys_role_menu、sys_role_user中间表的数据】
     *
     * @param roleIds
     * @return
     */
    @DeleteMapping("deleteRoleByIds/{roleIds}")
    public AjaxResult deleteRoleByIds(@PathVariable Long[] roleIds) {
        return AjaxResult.toAjax(this.roleService.deleteRoleByIds(roleIds));
    }

    /**
     * 根据用户id查询用户拥有的角色ids
     *
     * @param userId
     * @return
     */
    @GetMapping("getRoleIdsByUserId/{userId}")
    public AjaxResult getRoleIdsByUserId(@PathVariable Long userId) {
        List<Long> roleIds = this.roleService.getRoleIdsByUserId(userId);
        return AjaxResult.success(roleIds);
    }

    /**
     * 保存角色和用户之间的关系
     *
     * @param userId
     * @param roleIds
     * @return
     */
    @PostMapping("saveRoleUser/{userId}/{roleIds}")
    public AjaxResult saveRoleUser(@PathVariable Long userId, @PathVariable Long[] roleIds) {
        /**
         * 因为我们使用的是路径参数，前端传回的roleIds可能是一个空值，空值的话无法匹配上面的路径
         * 所以如果为空，需要让前端传回一个-1到该接口【如果是-1则说明当前角色一个权限也没有选择】
         */
        if (roleIds.length == 1 && roleIds[0].equals(-1L)) {
            roleIds = new Long[]{};
        } else {
            this.roleService.saveRoleUser(userId, roleIds);
        }
        return AjaxResult.success("保存成功");
    }


}
